Fixed off by 1 error, improved js