Added replaceLast and corresponding tests