fixing null pointer caused by logic ordering