Moved checking logic into its own function