moved around a bit for organization, commented some more for some explanations