Added a main and fixed some bugs.