Rearranged code to better separate component logic and rendering