Check if there is selected item at all.
Free filtered items.
Do bounds checking correctly.
Move side effects after cleanup.