added all necessary functions and comments