Remove Duplicate Code, Add Unit Tests, Fix Failing Tests