Final version that works as expected