General cleanup, comments, and addition of onError event