simplify the algorithm quite a bit...