Added extra size check to avoid out of bounds read.