Fixing some memory problems