implemented write_all
implemented main_loop