properly remove elements from array