Refactor to encoding method