Fixed some bugs and exceptions