A lot of code refix according to Paul's suggestion