Needs more testing and improving but appears to work - closes #2