cleaned up and moved to MVC style model