This contains updated code as asked by @NikolayKuznetsov