Many changes: code simplification, improve shutdown.