restructured code for readablity