handling ctrl-c and some restructuring