Working solution to parts 4/7 in programming assignment 3