changed some code for memory management.