Factoring out some duplicate code