diff --git a/py/asmx64.c b/py/asmx64.c
index 226d4efee852062610cf1b5cc7b28e98b092feef..d3158170fb118a4deb56536754a2aa5cb64f3162 100644
--- a/py/asmx64.c
+++ b/py/asmx64.c
@@ -1,16 +1,18 @@
 #include <stdio.h>
 #include <assert.h>
-#include <sys/types.h>
-#include <sys/mman.h>
 #include <string.h>
 
 #include "misc.h"
-#include "asmx64.h"
 #include "mpconfig.h"
 
 // wrapper around everything in this file
 #if MICROPY_EMIT_X64
 
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#include "asmx64.h"
+
 #if defined(__OpenBSD__) || defined(__MACH__)
 #define MAP_ANONYMOUS MAP_ANON
 #endif
diff --git a/py/gc.c b/py/gc.c
index 7d4cac6e74df3dee6958778ad9c9c4d198bb3d9a..054a3cc3153665b0da61814c7a864a6ae19851df 100644
--- a/py/gc.c
+++ b/py/gc.c
@@ -6,6 +6,8 @@
 #include "mpconfig.h"
 #include "gc.h"
 
+#if MICROPY_ENABLE_GC
+
 // a machine word is big enough to hold a pointer
 /*
 #define BYTES_PER_WORD (8)
@@ -380,3 +382,5 @@ int main(void) {
     gc_dump_at();
 }
 */
+
+#endif // MICROPY_ENABLE_GC
diff --git a/py/lexerunix.c b/py/lexerunix.c
index 14c28c16d92d2302901916db50520e810e8b1402..5336610baea7d4f6769307e9fd65e7c375a29703 100644
--- a/py/lexerunix.c
+++ b/py/lexerunix.c
@@ -4,8 +4,11 @@
 #include <fcntl.h>
 
 #include "misc.h"
+#include "mpconfig.h"
 #include "lexer.h"
 
+#if MICROPY_ENABLE_LEXER_UNIX
+
 typedef struct _str_buf_t {
     bool free;                  // free src_beg when done
     const char *src_beg;        // beginning of source
@@ -78,3 +81,5 @@ mp_lexer_t *mp_import_open_file(qstr mod_name) {
     vstr_printf(vstr, "%s.py", qstr_str(mod_name));
     return mp_lexer_new_from_file(vstr_str(vstr)); // TODO does lexer need to copy the string? can we free it here?
 }
+
+#endif // MICROPY_ENABLE_LEXER_UNIX
diff --git a/py/mpconfig.h b/py/mpconfig.h
index 8ce432a61ed304fc73798ddd969d6ade3a606351..2017ba366a64704ca808bd8708a0c107d011c063 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -39,14 +39,29 @@
 #define MICROPY_MEM_STATS (0)
 #endif
 
+// Whether to build code to show byte code
+#ifndef MICROPY_SHOW_BC
+#define MICROPY_SHOW_BC (0)
+#endif
+
 /*****************************************************************************/
 /* Fine control over Python features                                         */
 
+// Whether to include the garbage collector
+#ifndef MICROPY_ENABLE_GC
+#define MICROPY_ENABLE_GC (0)
+#endif
+
 // Whether to include REPL helper function
 #ifndef MICROPY_ENABLE_REPL_HELPERS
 #define MICROPY_ENABLE_REPL_HELPERS (0)
 #endif
 
+// Whether to include lexer helper function for unix
+#ifndef MICROPY_ENABLE_LEXER_UNIX
+#define MICROPY_ENABLE_LEXER_UNIX (0)
+#endif
+
 // Whether to support float and complex types
 #ifndef MICROPY_ENABLE_FLOAT
 #define MICROPY_ENABLE_FLOAT (0)
diff --git a/py/py.mk b/py/py.mk
index e673c713c7b32ef23759fdfeb81f7e8983b553c2..3ed8a3e3d71d98051cd2462e5b25885b23348a0e 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -24,6 +24,7 @@ PY_O_BASENAME = \
 	nlrx64.o \
 	nlrthumb.o \
 	malloc.o \
+	gc.o \
 	qstr.o \
 	vstr.o \
 	unicode.o \
@@ -88,6 +89,10 @@ $(PY_BUILD)%.o: $(PY_SRC)/%.S
 $(PY_BUILD)%.o: $(PY_SRC)/%.c mpconfigport.h
 	$(CC) $(CFLAGS) -c -o $@ $<
 
+# optimising gc for speed; 5ms down to 4ms on pybv2
+$(PY_BUILD)gc.o: $(PY_SRC)/gc.c
+	$(CC) $(CFLAGS) -O3 -c -o $@ $<
+
 # optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
 $(PY_BUILD)vm.o: $(PY_SRC)/vm.c
 	$(CC) $(CFLAGS) -O3 -c -o $@ $<
diff --git a/py/showbc.c b/py/showbc.c
index eb7d41b24dac30e1d491606e7d00983071f26d0a..aea0aff67a0399e45b441cb380eaea75f2f11fb6 100644
--- a/py/showbc.c
+++ b/py/showbc.c
@@ -8,6 +8,8 @@
 #include "mpconfig.h"
 #include "bc0.h"
 
+#if MICROPY_SHOW_BC
+
 #define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0)
 #define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
 #define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
@@ -363,3 +365,5 @@ void mp_show_byte_code(const byte *ip, int len) {
         printf("\n");
     }
 }
+
+#endif // MICROPY_SHOW_BC
diff --git a/stm/Makefile b/stm/Makefile
index cd998dd8826f1391057225d73f6ff1a216253384..fecd525276986282a12b7b55dd8bb6505c5074a0 100644
--- a/stm/Makefile
+++ b/stm/Makefile
@@ -1,9 +1,16 @@
+# define main target
+all: all2
+
+# include py core make definitions
+include ../py/py.mk
+
+# program for deletion
+RM = /bin/rm
+
 STMSRC=lib
 #STMOTGSRC=lib-otg
 FATFSSRC=fatfs
 CC3KSRC=cc3k
-PYSRC=../py
-BUILD=build
 DFU=../tools/dfu.py
 TARGET=PYBOARD
 
@@ -11,7 +18,7 @@ AS = arm-none-eabi-as
 CC = arm-none-eabi-gcc
 LD = arm-none-eabi-ld
 CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion -DSTM32F40XX -DHSE_VALUE=8000000
-CFLAGS = -I. -I$(PYSRC) -I$(FATFSSRC) -I$(STMSRC) -Wall -ansi -std=gnu99 -Os -DNDEBUG $(CFLAGS_CORTEX_M4) -D$(TARGET)
+CFLAGS = -I. -I$(PY_SRC) -I$(FATFSSRC) -I$(STMSRC) -Wall -ansi -std=gnu99 -Os -DNDEBUG $(CFLAGS_CORTEX_M4) -D$(TARGET)
 #CFLAGS += -I$(STMOTGSRC) -DUSE_HOST_MODE -DUSE_OTG_MODE
 LDFLAGS = --nostdlib -T stm32f405.ld
 
@@ -43,53 +50,6 @@ SRC_S = \
 	startup_stm32f40xx.s \
 	gchelper.s \
 
-PY_O = \
-	nlrthumb.o \
-	gc.o \
-	malloc.o \
-	qstr.o \
-	vstr.o \
-	unicode.o \
-	lexer.o \
-	parse.o \
-	scope.o \
-	compile.o \
-	emitcommon.o \
-	emitpass1.o \
-	emitbc.o \
-	asmthumb.o \
-	emitnthumb.o \
-	emitinlinethumb.o \
-	runtime.o \
-	map.o \
-	obj.o \
-	objbool.o \
-	objboundmeth.o \
-	objcell.o \
-	objclass.o \
-	objclosure.o \
-	objcomplex.o \
-	objdict.o \
-	objexcept.o \
-	objfloat.o \
-	objfun.o \
-	objgenerator.o \
-	objinstance.o \
-	objint.o \
-	objlist.o \
-	objmodule.o \
-	objnone.o \
-	objrange.o \
-	objset.o \
-	objslice.o \
-	objstr.o \
-	objtuple.o \
-	objtype.o \
-	builtin.o \
-	builtinimport.o \
-	vm.o \
-	repl.o \
-
 SRC_FATFS = \
 	ff.c \
 	diskio.c \
@@ -146,10 +106,10 @@ SRC_CC3K = \
 	ccspi.c \
 	pybcc3k.c \
 
-OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o) $(PY_O) $(SRC_FATFS:.c=.o) $(SRC_STM:.c=.o) $(SRC_CC3K:.c=.o))
+OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o) $(SRC_FATFS:.c=.o) $(SRC_STM:.c=.o) $(SRC_CC3K:.c=.o)) $(PY_O)
 #OBJ += $(addprefix $(BUILD)/, $(SRC_STM_OTG:.c=.o))
 
-all: $(BUILD) $(BUILD)/flash.dfu
+all2: $(BUILD) $(BUILD)/flash.dfu
 
 $(BUILD)/flash.dfu: $(BUILD)/flash0.bin $(BUILD)/flash1.bin
 	python $(DFU) -b 0x08000000:$(BUILD)/flash0.bin -b 0x08020000:$(BUILD)/flash1.bin $@
@@ -164,9 +124,6 @@ $(BUILD)/flash.elf: $(OBJ)
 	$(LD) $(LDFLAGS) -o $@ $(OBJ)
 	arm-none-eabi-size $@
 
-$(BUILD):
-	mkdir -p $@
-
 $(BUILD)/%.o: %.s
 	$(AS) -o $@ $<
 
@@ -185,31 +142,7 @@ $(BUILD)/%.o: $(STMSRC)/%.c
 $(BUILD)/%.o: $(CC3KSRC)/%.c
 	$(CC) $(CFLAGS) -c -o $@ $<
 
-$(BUILD)/%.o: $(PYSRC)/%.s
-	$(AS) -o $@ $<
-
-$(BUILD)/%.o: $(PYSRC)/%.S
-	$(CC) $(CFLAGS) -c -o $@ $<
-
-$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h
-	$(CC) $(CFLAGS) -c -o $@ $<
-
-$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
-	$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
-
-# optimising gc for speed; 5ms down to 4ms
-$(BUILD)/gc.o: $(PYSRC)/gc.c
-	$(CC) $(CFLAGS) -O3 -c -o $@ $<
-
-# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
-$(BUILD)/vm.o: $(PYSRC)/vm.c
-	$(CC) $(CFLAGS) -O3 -c -o $@ $<
-
-$(BUILD)/parse.o: $(PYSRC)/grammar.h
-$(BUILD)/compile.o: $(PYSRC)/grammar.h
-$(BUILD)/emitbc.o: $(PYSRC)/emit.h
-
 clean:
-	/bin/rm -rf $(BUILD)
+	$(RM) -rf $(BUILD)
 
-.PHONY: all clean
+.PHONY: all all2 clean
diff --git a/stm/mpconfigport.h b/stm/mpconfigport.h
index ee90db32182ffd7b414149abecda2e54cb0bc734..dfa46cc504d408554aa34c8bd5e26d7c664f3df2 100644
--- a/stm/mpconfigport.h
+++ b/stm/mpconfigport.h
@@ -4,6 +4,7 @@
 
 #define MICROPY_EMIT_THUMB          (1)
 #define MICROPY_EMIT_INLINE_THUMB   (1)
+#define MICROPY_ENABLE_GC           (1)
 #define MICROPY_ENABLE_REPL_HELPERS (1)
 #define MICROPY_ENABLE_FLOAT        (1)
 
diff --git a/unix-cpy/mpconfigport.h b/unix-cpy/mpconfigport.h
index 9e3e32bb44924370a25d26c40db23732e10c304a..3fc186677280634034f15356e8a6e23416f25968 100644
--- a/unix-cpy/mpconfigport.h
+++ b/unix-cpy/mpconfigport.h
@@ -1,7 +1,8 @@
 // options to control how Micro Python is built
 
-#define MICROPY_ENABLE_FLOAT        (1)
 #define MICROPY_EMIT_CPYTHON        (1)
+#define MICROPY_ENABLE_LEXER_UNIX   (1)
+#define MICROPY_ENABLE_FLOAT        (1)
 
 // type definitions for the specific machine
 
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index b7901069ac3b19fee66cb238f8f326d384cdcdce..832764121339d59a356a180542b2590f00cfb7de 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -10,6 +10,7 @@
 #define MICROPY_EMIT_INLINE_THUMB   (0)
 #define MICROPY_MEM_STATS           (1)
 #define MICROPY_ENABLE_REPL_HELPERS (1)
+#define MICROPY_ENABLE_LEXER_UNIX   (1)
 #define MICROPY_ENABLE_FLOAT        (1)
 
 // type definitions for the specific machine