Improve encoding.rb
Refactors some code