Big refactor, moved into separate files/module.
Fixed bug in prefix exceptions.