Rewrote into a nice function