Some refactorings and improvements.