Master version of #312