Bunch of small fixes copied from old version