Proper cleanup of some resources
Fixed memory leaking issues