Avoid accessing undefined indexes

Signed-off-by: Madhura Jayaratne <madhura.cj@gmail.com>