Expand ranges and fix bug