refactoring and removed bug