Refactor almost everything, encoding still gives headaches