Working towards some much tidier code. Still a couple of bits left to implement before writing actual API code
Fixed conflicts and merged changed from last night.