Refactor and added a bunch of of functions