Converted a few lines of duplicate code to a function.