[Refactoring] Fixed potential null reference exception.