More fixes and refactors