refactored the code structure