refactored by @anoopsjohn comments