Removed some unnecessary code, improved exception throw order, bug fix.