Fully working version with the exception of terminate