Code refactoring and a bit of bugfixing. Testing tomorrow.