Improved updating the history file in the case when multiple shells are

simultaneously using the function.

Note: this can still be improved further as simultaneous updates can cause
one to get dropped.