Avoid invalid premature downcasts

Change-Id: Ifc6d84a63719b141ddc9bd89349d4a605d2b3e00