Thread pool code based on a blog by [Kim Burgestrand]https://github.com/Burgestrand