Added additional condition to prevent variable from being accessed when it hasn't been set.