The command \flushlayer is used to immediately typeset the contents of a layer and empty it.
All issues with: