First working version, taken from ongoing project