Changes from dselassi's code review (on github), thanks! Also added performance test