Refactor codebase to a more functional style