Now fully working, needs loads of refactoring still.