fixed some bugs and added error message