Fixed memory bugs and gravity bugs