Added some comments to make clearer what functions are doing