refactoring and re-organizing the code