Modified functions - but not yet commented
Complete, with comments