the predicate would be better off as an assert; also simplified the algorithm a lot..
added commentary