working version with range structure