Made a lot of changes in memory allocation