Update buggy javscript loop