Some refactoring

Should help keep things sane.