Memory management implemented. Needs testing and optimization.