redesign the code logic