some memory bugs fixed