fix for prevIndex going negative