Wrote weight funciton