refactored with minimal changes