avoid double free and fix signal handling

fix #9, #10, #11