diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b6f707b0..89e92c0f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -48,15 +48,19 @@ set(AVRDUDE_EXTERNAL_PATH "${PROJECT_SOURCE_DIR}/external")
 find_package(FLEX)
 if(FLEX_FOUND)
     FLEX_TARGET(Parser lexer.l "${PROJECT_BINARY_DIR}/lexer.c")
+
+    if (FLEX_VERSION VERSION_GREATER_EQUAL 2.5.9)
+        set(HAVE_YYLEX_DESTROY 1)
+    endif()
 else()
-    message(SEND_ERROR "This CMake project requires flex, which is not installed on your system." )
+    message(SEND_ERROR "This CMake project requires 'flex', which is not installed on your system." )
 endif()
 
 find_package(BISON)
 if(BISON_FOUND)
     BISON_TARGET(Parser config_gram.y "${PROJECT_BINARY_DIR}/config_gram.c" DEFINES_FILE "${PROJECT_BINARY_DIR}/config_gram.h")
 else()
-    message(SEND_ERROR "This CMake project requires bison, which is not installed on your system." )
+    message(SEND_ERROR "This CMake project requires 'bison', which is not installed on your system." )
 endif()
 
 # =====================================
@@ -121,6 +125,11 @@ if(HAVE_LIBUSB_1_0)
     set(LIB_LIBUSB_1_0 ${HAVE_LIBUSB_1_0})
 endif()
 
+# FreeBSD's library 'libusb' supports both the libusb-0.1 and libusb-1.0 API.
+if (HAVE_LIBUSB AND CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
+    set(HAVE_LIBUSB_1_0 ${HAVE_LIBUSB})
+endif()
+
 find_library(HAVE_LIBUSB_WIN32 NAMES libusb0.a usb0)
 
 if(HAVE_LIBUSB OR HAVE_LIBUSB_1_0 OR HAVE_LIBUSB_WIN32)
@@ -182,6 +191,8 @@ endif()
 # Setup target specific options
 # =====================================
 
+add_compile_definitions(CONFIG_DIR=\"${CONFIG_DIR}\")
+
 if(WIN32)
     set(HAVE_LIBWS2_32 1)
     set(EXTRA_WINDOWS_SOURCES "${PROJECT_BINARY_DIR}/windows.rc")
@@ -318,8 +329,22 @@ message(STATUS "----------------------")
 # Configure files
 # =====================================
 
+macro(configure_option option)
+    if(${${option}})
+        string(REGEX REPLACE "(.*)@${option}_BEGIN@(.*)@${option}_END@(.*)" "\\1\\2\\3" conf_file "${conf_file}")
+    else()
+        string(REGEX REPLACE "(.*)@${option}_BEGIN@(.*)@${option}_END@(.*)" "\\1\\3" conf_file "${conf_file}")
+    endif()
+endmacro()
+
+file(READ avrdude.conf.in conf_file)
+configure_option(HAVE_PARPORT)
+configure_option(HAVE_LINUXGPIO)
+configure_option(HAVE_LINUXSPI)
+file(WRITE "${PROJECT_BINARY_DIR}/avrdude.conf.in" "${conf_file}")
+
 configure_file(cmake_config.h.in ac_cfg.h)
-configure_file(avrdude.conf.in avrdude.conf)
+configure_file("${PROJECT_BINARY_DIR}/avrdude.conf.in" avrdude.conf)
 configure_file(avrdude.spec.in avrdude.spec)
 if(WIN32)
     configure_file(windows.rc.in windows.rc)
diff --git a/src/avr.c b/src/avr.c
index 4091b497..dd9095e4 100644
--- a/src/avr.c
+++ b/src/avr.c
@@ -331,7 +331,7 @@ int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype,
 
   /* supports "paged load" thru post-increment */
   if ((p->flags & AVRPART_HAS_TPI) && mem->page_size > 1 &&
-      pgm->cmd_tpi != NULL) {
+      mem->size % mem->page_size == 0 && pgm->cmd_tpi != NULL) {
 
     while (avr_tpi_poll_nvmbsy(pgm));
 
@@ -361,7 +361,8 @@ int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype,
     return avr_mem_hiaddr(mem);
   }
 
-  if (pgm->paged_load != NULL && mem->page_size > 1) {
+  if (pgm->paged_load != NULL && mem->page_size > 1 &&
+      mem->size % mem->page_size == 0) {
     /*
      * the programmer supports a paged mode read
      */
diff --git a/src/avrftdi.c b/src/avrftdi.c
index dc3ab939..654614f6 100644
--- a/src/avrftdi.c
+++ b/src/avrftdi.c
@@ -71,7 +71,7 @@ enum { FTDI_SCK = 0, FTDI_MOSI, FTDI_MISO, FTDI_RESET };
 static int write_flush(avrftdi_t *);
 
 /*
- * returns a human-readable name for a pin number. the name should match with
+ * returns a human-readable name for a pin number. The name should match with
  * the pin names used in FTDI datasheets.
  */
 static char*
@@ -125,7 +125,7 @@ ftdi_pin_name(avrftdi_t* pdata, struct pindef_t pin)
 }
 
 /*
- * output function, to save if(vebose>level)-constructs. also prefixes output
+ * output function, to save if(vebose>level)-constructs. Also prefixes output
  * with "avrftdi function-name(line-number):" to identify were messages came
  * from.
  * This function is the backend of the log_*-macros, but it can be used
@@ -163,9 +163,9 @@ void avrftdi_log(int level, const char * func, int line,
 }
 
 /*
- * helper function to print a binary buffer *buf of size len. begin and end of
- * the dump are enclosed in the string contained in *desc. offset denotes the
- * number of bytes which are printed on the first line (may be 0). after that
+ * helper function to print a binary buffer *buf of size len. Begin and end of
+ * the dump are enclosed in the string contained in *desc. Offset denotes the
+ * number of bytes which are printed on the first line (may be 0). After that
  * width bytes are printed on each line
  */
 static void buf_dump(const unsigned char *buf, int len, char *desc,
@@ -531,7 +531,7 @@ static int avrftdi_check_pins_bb(PROGRAMMER * pgm, bool output)
 	/* value for 8/12/16 bit wide interface */
 	int valid_mask = ((1 << pdata->pin_limit) - 1);
 
-	log_debug("Using valid mask bibanging: 0x%08x\n", valid_mask);
+	log_debug("Using valid mask bitbanging: 0x%08x\n", valid_mask);
 	static struct pindef_t valid_pins;
 	valid_pins.mask[0] = valid_mask;
 	valid_pins.inverse[0] = valid_mask ;
@@ -556,7 +556,7 @@ static int avrftdi_check_pins_mpsse(PROGRAMMER * pgm, bool output)
 
 	avrftdi_t* pdata = to_pdata(pgm);
 
-	/* SCK/MOSI/MISO are fixed and not invertable?*/
+	/* SCK/MOSI/MISO are fixed and not invertible?*/
 	/* TODO: inverted SCK/MISO/MOSI */
 	static const struct pindef_t valid_pins_SCK  = {{0x01},{0x00}} ;
 	static const struct pindef_t valid_pins_MOSI = {{0x02},{0x00}} ;
@@ -705,7 +705,7 @@ static int avrftdi_open(PROGRAMMER * pgm, char *port)
 	if(err) {
 		log_err("Error %d occurred: %s\n", err, ftdi_get_error_string(pdata->ftdic));
 		//stupid hack, because avrdude calls pgm->close() even when pgm->open() fails
-		//and usb_dev is intialized to the last usb device from probing
+		//and usb_dev is initialized to the last usb device from probing
 		pdata->ftdic->usb_dev = NULL;
 		return err;
 	} else {
@@ -831,7 +831,7 @@ static int avrftdi_initialize(PROGRAMMER * pgm, AVRPART * p)
 
 		/*setting rst back to 0 */
 		set_pin(pgm, PIN_AVR_RESET, OFF);
-		/*wait at least 20ms bevor issuing spi commands to avr */
+		/*wait at least 20ms before issuing spi commands to avr */
 		usleep(20 * 1000);
 	}
 
@@ -840,7 +840,7 @@ static int avrftdi_initialize(PROGRAMMER * pgm, AVRPART * p)
 
 static void avrftdi_display(PROGRAMMER * pgm, const char *p)
 {
-	// print the full pin definitiions as in ft245r ?
+	// print the full pin definitions as in ft245r ?
 	return;
 }
 
@@ -1045,7 +1045,7 @@ static int avrftdi_flash_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
 		bufptr += 4;
 	}
 
-	/* find a poll byte. we cannot poll a value of 0xff, so look
+	/* find a poll byte. We cannot poll a value of 0xff, so look
 	 * for a value != 0xff
 	 */
 	for(poll_index = addr+len-1; poll_index > addr-1; poll_index--)
diff --git a/src/cmake_config.h.in b/src/cmake_config.h.in
index 533c8475..9643546e 100644
--- a/src/cmake_config.h.in
+++ b/src/cmake_config.h.in
@@ -22,8 +22,6 @@
 
 #define VERSION "@PROJECT_VERSION@"
 
-#define CONFIG_DIR "${CONFIG_DIR}"
-
 /* Options */
 
 /* Linux sysfs GPIO support enabled */
diff --git a/src/jtag3.c b/src/jtag3.c
index 9d6d5604..0c04426a 100644
--- a/src/jtag3.c
+++ b/src/jtag3.c
@@ -430,6 +430,7 @@ int jtag3_send(PROGRAMMER * pgm, unsigned char * data, size_t len)
   if (serial_send(&pgm->fd, buf, len + 4) != 0) {
     avrdude_message(MSG_INFO, "%s: jtag3_send(): failed to send command to serial port\n",
                     progname);
+    free(buf);
     return -1;
   }
 
diff --git a/src/jtagmkI.c b/src/jtagmkI.c
index fc5f3f70..2d4d83cd 100644
--- a/src/jtagmkI.c
+++ b/src/jtagmkI.c
@@ -214,6 +214,7 @@ static int jtagmkI_send(PROGRAMMER * pgm, unsigned char * data, size_t len)
   if (serial_send(&pgm->fd, buf, len + 2) != 0) {
     avrdude_message(MSG_INFO, "%s: jtagmkI_send(): failed to send command to serial port\n",
                     progname);
+    free(buf);
     return -1;
   }