Renamed some vars, restructured slightly