working js implementation