Fix a couple of bugs and remove redundant function.