Refactored and tidied up ipgrammar.js.