Documented functions and cleaned up code