Turned some of the pseudo code into actual code