Major improvements; including modularising some code and putting it into
the Lib repo.