Moved logic to fix inifinite loop