modify code based on comments from Lian Cheng