Finally working.  Added some tests.  Still some tidying up required