Add terminate function