Refactoring, bugfixes, error handling.