Refactoring code into funciton.