Fixes #1 and fixes #2. Also cleaned up memory leaks and all warnings.