Refactor code to be a method