Rewrote whole code