Factor out scaling algorithms. Add tests.