Cleaned up code, ran through valgrind to fix some memory issues