refactoring the code for better understanding.