Better algorithm for determining scales