Changed memory allocation algorithm