Fixing crashes caused by incorrect logic