fixing some issues and general code cleanup. moving logic  out of html files