Cleanup duplicated code a bit