fixes, refactoring and cleanup