Fixed some exceptions and bugs.