refactored and small bugfixes