Flexible distributed generation and demand to enhance power system stability