Wrote most of the functions