Added somewhat more verbose error handling, added javadocs.