refactor JS functions for simplicity