refactoring ugly code