Code for parts 2 and 3 mostly