Near-optimal number of flips calculated
A little re-factoring.