Fixed some bugs and added Debug option