Improved code legibility.
Fixed a refactoring bug. Improved exception message.
Added more comments, standardised function names.
Added specific Exception class.
Used proper exception class, not generic.