Improved input validation
Improved termination