Replaced the type cast (twice with 'is) with better syntax (once with 'as')

When checking a variable with 'is' and then casting it results in the type being checked twice.
Replaced it with 'as' and then checking for null. This results in the type only being checked once.