Code cleanup and a few minor api changes (sorry)