Use python dict to store data and improve function