Changed the logic algorithm to a more sensible setup.