refactored table checking code