Improve performance, fixing #342