add handling signals
fix logging
wait for file writing to finish