final solution for #14, still a wee bit slow, but not terrible