added error msg and fixed bugs