Add test and refactored error code