Moved cleanup code to proper place.