Moved duplicate code out into a method