Fixed some bugs and improved code base