Proper error checking (fixes #20)