Cleanup code related to quality layer allocation, and add a few safety checks