Add a main file and fix some bugs.