Refactor common code to util.js.