refactor grammar, fix solve ordering