refactoring the original code