fix bugs. First working version