How do motors and generators work?