Put in some checks on length of list to avoid indexoutofbounds exception.