simplified code, fixed flickering