tested, added error reporting and debug output