Made smaller functions, added comments.