refactored the code for simplicity