refactoring and fixing few bugs