rewrote code nicer