got encode working
code cleanup + reformatting