Working version after some bugs