First working version; debugging needed