From the looks of it this is the same as the above, where I was modifying an outdated version of the source. The only relevant changes are on lines 147-157; my apologies for not catching those minor changes.