cleaner code, added code for section 2.6 & 2.7