Cleaned-up the variable names and fixed a few bugs.