Added restore logic and fixed a few bugs

Change-Id: Ic6148065d699c4a4c8a00fa80e9cd239a3f606b1