There's a lot of repetition in this file, and it could probably stand to be DRYed out, but we'd been coding for about three hours by the time we submitted the PR and we both needed a break. All suggestions welcome!