@matty: The two functions you posted there are for getting the low- and high-order bytes, not the low- and high-order words! A word consists of 2 bytes (16 bits), so you need to alter your functions a bit.
js code:
function loword(n){ return n & 0xffff; }
function hiword(n){ return (n >> 16) & 0xffff; }