Fixed logic flaws in previous commit