Changed code to perform early exit when grammar has not changed
Applied minor refactorings to ease readability