Moved some duplicate code into helper functions