js: First draft with correct behavior