restructured js code to reflect a MVC model