edited code to address comments