reorganizing and refactoring the code