Smushed the logic a bit to reduce the repetition.