restructure the code and logic