refactoring some duplicated code