Fixing logic error on swap condition