bugfixing and refactoring