rewrote logic using dictionaries