Seems to work now, still need to check against php output.