Rather large refactor, move stuff into different files