Added threading support. Speed-up is at least 2x