finally adapted to new API, not tested thorougly yet