more refactoring and corrections