Cleaned up code

Added documentation, removed dead code, fixed variable names, etc.