Substantial re-write as program with an eye for making it into a library