Modified so it can be called from a service