Refactor rivulet.js for readability