Moved code from controllers to services.
Added helper methods for integration tests.