revised stack deleting logic