Changed dictionary format and added comments for you to see my logic.