Now, this is a beautiful algorithm.