Various fixes from master

Updated this pull to include usefull fixes from master.