improve error handling; fix test mechanism