Refactor, moving functions into their own files for clarity