big refactor for the functions to make better logic flow