Credit to Jonathan Paulson, implemented in CS