refactored the code