Use pattern matching instead of accessing the lists elements