fixed last value returning undefined