Sorry if my code is a little messy, hope the notes make it easier to read