Added validation before casting