Bulletproof type-checking and helper functions