Filling in 3-flip algorithm, and some driver code