fixed flawed logic