First version that seems to completely work