Fixed bug when removing elements from an array