formatted and commented code, removed debugging console outputs.