Fixed a few bugs, and removed a memory leak.