fixed numerous major bugs and performance problems. Still need to write unit tests.