tidy up of code, commit before debug changes, one apply function