simplified math and got rid of loop