refactor shared code into methods.