Better fix for #7, still needs cleaning up