Add condition to incoparate array and hash and optimize code