working version; debugging needed;