Improved error message.
Added docs, refactored code.