Fixed some bugs with DB merging and added more safety checks