a better version of the solver. taken from norvig