Initial code by M. Taylor (2008)