Cleanup split helper function
Big refactor, splitting things up and cleaning the code.