Lotsa refractoring. Passes all tests.