restructured code for better readability