add qunit test, improved code with better failure handling