Some fixes after discovering problems while coding other stuff with similar code