Modifications to the code to support while loop generation (correctly?)