Attempt fix for #947

Append and removal code appears to have been counting incorrectly for both adding and removal. Here the length of the number of items is used to append new items, and the removal position adjusted to find the last item instead of second last.