added test & minor optimization