Fixed a couple bugs. Added a better error message handler.