Improved commenting, fixed shutdown code