checking memory leaks

checking code to make it a little better handling variables, and freeing
memory