major bug fixes in memory allocation