Reorganize code, make an input thread