some working code -- needs factoring
tests passing