heavy refactoring. Working Encode.hs