Maybe use $status instead of $result.