refactoring and polishing code