Fix some bugs in the existing code