refactoring the code and fixed #10