bugfixes and some refactoring with events/errors