Fixed error checking and out of bounds