Upon reflection, simplified code somewhat