Major refactoring, make things RESTFUL and tidy code