Fixed bugs and added unit tests for memory.c