Fixed a bug in logic