Refactor this a bit. Now stuff fails on OS X