Reformatted the code and commented more heavily.