What should your remaining average cost be?
At the moment I do this (pseudocode), for a sale, the number of shares is negative:
price_per_share = 0;
Repeat for all transactions of the same stock symbol;
price_per_share = price_per_share + number_of_shares*stock_price/number_of_shares;
End repeat;