Fix a bug, rename some variables, add some docs