C++ code from Otago Polytechnic 2014