Improved exception handling. Added some javadoc.