Bugfix + little refactoring