write top level loop and sketch inner functions
notes from Tom