Refactored the check function to be reused