Code refactoring

Code refactoring
Bugs correction
Few improvements