Check if layer is valid before using it