refactoring and code deduplication