refine code according to Sean's feedback.