Second iteration. Refactorings and improvements