the solution I came up with some time ago: http://stackoverflow.com/a/7060342/114795