added parts 1-4 of HW6, so far unoptimized