initial code version, authored by Justin Mason