Introduced HandleScope to cleanup memory properly, fixed other minor
memory leaks