Refactor and pull out logic to functions

Update the APIs for user UI data to pull out the logic from the APIs to
make the code a bit more readable.