Cleaned js code. Improved infinite looping