Added comments and DRYed up code