Added more comments to the function to help with readibility.