checking read/write permissions. fixes #189, pull request by Jeff Zhang