Fix list index out of range exception