modified merge code to be a bit more powerful, fixed some bugs.