implemented proper clean shutdown, plus misc other improvements