improving performance with some custom threading