The error handling seems to be unwound into each case explicitly. This function could benefit from goto considered helpful error handling.