cleaned up ReplaceTable and added tests