Fix and code refactoring