finished first algorithm, some utilities; half of second