Initial refactoring
Adds code to deal with functions other than clock
Various fixes arising from testing