Refactor UI code a bit