trying to cleanup this encoding nightmare