Implement thread, and refact some