cleanup, refactoring and fixes