javascript loop fix