Refactoring and few fixes