Cleaned up casting logic