modified to correct bug in the code