Better bounds checking, proper handling of missing $HOME

Created util.[ch] to hold common utilities.