Appears to be functionally working but needs far more tests and a tidy up.