refactoring part of the code