1) Fixing potential leaked file pointer.
2) Fixing a couple of potential leaked memory issues.
3) Adding a little more descriptive error messages.
4) Adding file write checks in merge functionality.