Add test cases and refactor some logic in cell.py