faster implementation closes #1