Added some missing error handling.
Minor typographical and bug fixes.