fixed bug in memory handling