Several improvements and bug fixes