Restructure main code, separate into multiple files