Some fixes and refactors.