Fixed off by one errors in js