Minor refactors and bug fixes.