Extract method setCleared from If statements…

Same duplicated code was used in two different if conditions, which has
been extract into a new method setCleared.