Added tests. Improved a little bit the performance