Refactored to DRY it up a little