updated functions and cleaned up code