working version, need bugfix & optimization