another refactoring and bug-fixes and debug-log (must be removed)