Some small refactorings. Fixed the NullReferenceException.