move duplicate code to a separate method