a more logically correct implementation