According to an example aspect of the present invention, there is provided a method for recovering nutrients and energy from sewage sludge and animal manure in an efficient manner by combining unit operations of drying, thermal gasification and ammonia and phosphorus recovery. In addition, a tailored fertilizer is provided from the recovered ammonia and phosphorus.