error checking and improving the flexibility