Database fix - Rebased for latest changes

@AlexSYanai: Ok, this one has been rebased to be current.