Adding proper shutdown handling