Added another check for list length.

This prevents a call to (lists:nth ...) with a zero-length list (which results
in an error).