Various fixes and patches from Bogdan