Refactoring findFirst pattern into function