Commented and reorganised code