Merge "Fix some HandleScope bugs and add corresponding checks"