added tests and a bit more sophisticated detection