Add checking function and polish the other functions
Add some error control