Write main alhoritm