Extracted out repeated check into a function.