Add unit tests
Fix priority merging logic and add unit test