Fix some error handling and variable naming