Added exception class for failed type checks
Removed duplicate code