Improve metadata merging, add tests for beforenotify callback