added more comments, function has been tested and works