Refactoring

Fixing potential NullReferenceExceptions.