implementing controller loop