improved the coding logic