Corrected code to take care of bug