various fix thanks to alexf!
merged and refiend