Changed exeption handling to proper exeptions.