Fixed the rewriter and the bug of the for loop. Need to check/test barrirers