avoid crash by avoiding accessing array with index -1.