fix bug ignoring last bin and check for -1 out of bounds