i would refactor this into a utils.js or something that can be called from multiple places.