Tidied the code a little and replaced the use of tolower with strcasecomp, as advised by @jcs.