Fixed NPE due to missing cleanup of stale elements.

Change-Id: Id2192e2951d07d03a53abc7ba7c1069a3224878c