Fixed serious bugs in memory allocator
Issue #45