Refine the code according to Matteo and Jon's comments.