simplified code slightly