fixed index out of bounds error - thanks to markus-mnm