Refactor sys_jail() and fix bugs.