Refactor UI code