added error handling, fixed test that in some cases caused to bugs