This moves out code that links/unlinks imported toplevels and child surfaces in functions to improve code readability.