Reorginized code for readablity