Improvements to memory allocation algorithm