Fix bug, use ternary to prevent trying to access a property on an undefined object.