Tons of refactor and tests