C++ version copyright James Matthews, 2003