Some fixes. Needs to be tested.