fixed a bunch of memory problems and other bugs