Update for 4.0.5b and fix bugs