Add explanation and fix some bugs