Refactoring code into functions