Lot of improvements and refactoring