Some improvement, thrown exception and added tests