Refactor

A bunch of refactoring.