[WIP] Refactor loader resolve()

Preliminary step to move all encoding logic out into its own method. Pushed all the encoding logic into the last for-loop and attempted to improve the readability of the code because I was getting too confused in here.