Move a lot of code around, refactor some stuff.