Fixed off-by-one error in dynamic implementation, better tracking of selected elements