added check for empty $key and $val values