Refactor grammar