Improve error handling on system calls