begin implementing steps 8, 9
replace encoding and decoding with sort_by_key