fixed/tweaked error handling