Fix wrong result in previous version but still very slow