Tracked down a math bug that I'm shocked I didn't see sooner (outputs under certain conditions are very obviously wrong). The C++ code I based this on had it right and I broke it during the porting to Javascript. Accidentally used the same index twice instead two different indices for one of the intermediate calculations.