.js clean up: rewrite the log/replace function