Cleaned and commented javascript. Final