def idx first, fix bug