Refactoring and some iprovements