Changed foldLeft to collectFirst to prevent unnecessary iterations