Sweeping code written by Jean-David Benamou