move some complicated logic out into helper functions.
refactor duplicate code into single helper.
Merge pull request #32 from ledbettj/cleanup

Cleanup