updated, commented and formatted