refactored and added accept and handle_connection functions