added signal handling and exit cleanup