Improve performances and add some tests