[WIP] First swing at refactoring for appropriate error codes