more robust grammar detection