rewritten with ctype library