move duplicating code into a method