Various improvements and bug fixes