better termination, better error handling