Output buffering added to avoid write response before headers sent