improved the performace and added more tests