batch bugfixing and refactoring