Finished code for all 4 questions