more refactoring and fix minor bugs