Several improvements and bug fix