Based on the code provided in Creative Programming for Digital Media & Mobile Apps 
by Dr Marco Gillies, Dr Matthew Yee-King, Dr Mick Grierson