added signal handling for proper termination