Cleanup code to make it more DRY