Re-worked code to share common functionality and (arguably) reduce complexity.