Refactor to be more functional friendly