Fixing critical logic error