Fixed some memory issues and polished up some code.