Fixing code according to J.K's recommendation