Changed asserts to exceptions

Handle illegal states with exceptions instead of asserts.