moved some duplicate code into its own method