improved input validation and cleaned up/improved comments