finished JS version of script