Plug error when issue #14 happens.

Also refactor code and comments a bit.