Refactors code to be more functional