Mostly working implementation