fixed bugs, working version