Refactored source to minimize code duplication