Changes proposed by Daniel Lopez and many refactorings