Fixed some NullReferenceExceptions. Small refactoring.