Replace some unsafe assertions with if-statements