Code tidy fixed global leak and tweaked some functions.