substract one from the end index. It's necessary to avoid end of vector errors