Minor refactoring and null checks