Fix few memory handling issues