Rewrote from scratch