Hopefully working and generic solution.