updated code based on Arthur's comments