modified Meg's script