Full refactor

Moved code around.