remove redundant code
more refactoring. prefer isolated scope. fix minor bugs