Enormous refactor. Now it's clean.