Added functions and restructured