Refactoring and some improvements.