Fixed the bugs, and improved maintaniablity.