fix the code according to Tom's comments