Fixed logic mistakes