fixed some memory problems though some remains