There's a lot of duplication in here but figured since the final UI will be in html there's not much point spending time cleaning it up.