Abstract some duplicate code