Added check to make sure that necessary variables are not null