Initial version of the logic, untested