Saving some memory and fixing some leaks