Improved layer instance tracking code.