update README.md
Refactoring and add error checking