refactor some logging and nil handling