Working version, needs some improvements