Fixed exception handling and tests