Seemingly working randomized version that seems to be reasonably fast.