Slight changes may occur as this API is stabilized. This PR is based on work previously done by DerFrZocker in #938.