F5: rewrriten mainLoop as in Perry's paper
working on F5 debugging