cleaned up some code, added signal handlers to exit gracefully