made the code more efficient, NOT TESTED