Cleaned up old code, refactored some variables for clarity.

Clean up and fixes.