Refactored to get rid of repetition