trying to sort out gate/loop