now working for the backward case as well