divide function refactered