General improvements and bug fixing