Fixed bugs in error handling.

Removed some redundamt code, got rid of some bugs when there unexpected
input.