added functions, explained stuff better in comments