Moved clearCanvas() to fix race

This fixes a race between binaryGet() and clearCanvas().
It also removes duplicate code