First working version, loads of refactoring left to do