Mostly done converting MATLAB prototype to JS