Fixes Null References Error and small refactoring