Refactor functional code to clean code