Refactor a couple of JS functions