Moved casting to existing conditional as it will break either way if the type is wrong.
Simplified test to ensure compatibility