Various changes and bug fixes