refactoring phase 1
more refactoring and comments