Code improvment for 8th problem