Refactored a bunch of shit. Fixed a bunch of shit. Added to API.
Refactored common code. This should have been in the previous commit.