Dirty version is now working. Code needs to be tidied up considerably.