Rewriting in JavaScript, way more simple