First apparently fully working version. Needs some code tidying but good enough for testing