Cleanup code and shrink state management