simplified code, but same idea