Working solution for part 8-10 of programming assignment 3