jail code reorganization and bug fix