Moar refactors and fixes