Found a solution and posted it on stackoverflow:

solution