[algorithms][Kashish] refactoring and fixing one bug