Many small code fixes