reorganize memory management functions and add function docs