Fix system calls, add exception handling