Fixed some errors in documentation.
Renamed some variables and parameters for clarity and consitency.
Removed some dead code.