Removed unnecessary globals. Fixed Daniel's sloppy code that had problems.