Working version, but found possible bug