rewrote into one function