Refactor JS to improve quality