updates from @erictj
Refactor to clean up duplicate code