diff --git a/py/builtin.c b/py/builtin.c
index 078f4b49c3843bab74cebb609fa5205b2d32fefa..389274f3196398c9958800e92756a3c79533bd14 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -18,7 +18,7 @@
 // args[0] is function from class body
 // args[1] is class name
 // args[2:] are base objects
-mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
+static mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
     assert(2 <= n_args);
 
     // we differ from CPython: we set the new __locals__ object here
@@ -62,7 +62,7 @@ mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
 
 MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
 
-mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
+static mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
     if (o != mp_const_none) {
         mp_obj_print(o);
         printf("\n");
@@ -70,6 +70,8 @@ mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
     return mp_const_none;
 }
 
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
+
 mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
     if (MP_OBJ_IS_SMALL_INT(o_in)) {
         mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
@@ -97,7 +99,9 @@ mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
     }
 }
 
-mp_obj_t mp_builtin_all(mp_obj_t o_in) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
+
+static mp_obj_t mp_builtin_all(mp_obj_t o_in) {
     mp_obj_t iterable = rt_getiter(o_in);
     mp_obj_t item;
     while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
@@ -108,7 +112,9 @@ mp_obj_t mp_builtin_all(mp_obj_t o_in) {
     return mp_const_true;
 }
 
-mp_obj_t mp_builtin_any(mp_obj_t o_in) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
+
+static mp_obj_t mp_builtin_any(mp_obj_t o_in) {
     mp_obj_t iterable = rt_getiter(o_in);
     mp_obj_t item;
     while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
@@ -119,7 +125,9 @@ mp_obj_t mp_builtin_any(mp_obj_t o_in) {
     return mp_const_false;
 }
 
-mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
+
+static mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
     if (mp_obj_is_callable(o_in)) {
         return mp_const_true;
     } else {
@@ -127,7 +135,9 @@ mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
     }
 }
 
-mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
+
+static mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
     int ord = mp_obj_get_int(o_in);
     if (0 <= ord && ord <= 0x10ffff) {
         char *str = m_new(char, 2);
@@ -139,7 +149,9 @@ mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
     }
 }
 
-mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
+
+static mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
     if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
         mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
         mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
@@ -152,6 +164,8 @@ mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
     }
 }
 
+MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
+
 static mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
     // TODO hash will generally overflow small integer; can we safely truncate it?
     return mp_obj_new_int(mp_obj_hash(o_in));
@@ -165,7 +179,7 @@ static mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
 
 MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
 
-mp_obj_t mp_builtin_len(mp_obj_t o_in) {
+static mp_obj_t mp_builtin_len(mp_obj_t o_in) {
     mp_obj_t len = mp_obj_len_maybe(o_in);
     if (len == NULL) {
         nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
@@ -174,7 +188,9 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) {
     }
 }
 
-mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len);
+
+static mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
     if (n_args == 1) {
         // given an iterable
         mp_obj_t iterable = rt_getiter(args[0]);
@@ -201,7 +217,9 @@ mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
     }
 }
 
-mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_max_obj, 1, mp_builtin_max);
+
+static mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
     if (n_args == 1) {
         // given an iterable
         mp_obj_t iterable = rt_getiter(args[0]);
@@ -228,6 +246,8 @@ mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
     }
 }
 
+MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
+
 static mp_obj_t mp_builtin_next(mp_obj_t o) {
     mp_obj_t ret = rt_iternext(o);
     if (ret == mp_const_stop_iteration) {
@@ -239,7 +259,7 @@ static mp_obj_t mp_builtin_next(mp_obj_t o) {
 
 MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
 
-mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
+static mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
     const char *str = qstr_str(mp_obj_get_qstr(o_in));
     if (strlen(str) == 1) {
         return mp_obj_new_int(str[0]);
@@ -248,15 +268,19 @@ mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
     }
 }
 
-mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
+
+static mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) {
+    assert(2 <= n_args && n_args <= 3);
     switch (n_args) {
         case 2: return rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]);
-        case 3: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
-        default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "pow expected at most 3 arguments, got %d", n_args));
+        default: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
     }
 }
 
-mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
+
+static mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
     for (int i = 0; i < n_args; i++) {
         if (i > 0) {
             printf(" ");
@@ -273,21 +297,25 @@ mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
     return mp_const_none;
 }
 
-mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_print_obj, 0, mp_builtin_print);
+
+static mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) {
+    assert(1 <= n_args && n_args <= 3);
     switch (n_args) {
         case 1: return mp_obj_new_range(0, mp_obj_get_int(args[0]), 1);
         case 2: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), 1);
-        case 3: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]));
-        default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "range expected at most 3 arguments, got %d", n_args));
+        default: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]));
     }
 }
 
-mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range);
+
+static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
+    assert(1 <= n_args && n_args <= 2);
     mp_obj_t value;
     switch (n_args) {
         case 1: value = mp_obj_new_int(0); break;
-        case 2: value = args[1]; break;
-        default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "sum expected at most 2 arguments, got %d", n_args));
+        default: value = args[1]; break;
     }
     mp_obj_t iterable = rt_getiter(args[0]);
     mp_obj_t item;
@@ -296,3 +324,5 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
     }
     return value;
 }
+
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
diff --git a/py/builtin.h b/py/builtin.h
index db7d517a06b3da3354bcabc2d094d0ffdadead4e..0198d63bfd08bf4a0783f3342bca1ad6058720b1 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -1,25 +1,24 @@
-// TODO convert all these to objects using MP_DECLARE and MP_DEFINE
+mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args);
 
 MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj);
-mp_obj_t mp_builtin___import__(int n, mp_obj_t *args);
-mp_obj_t mp_builtin___repl_print__(mp_obj_t o);
-mp_obj_t mp_builtin_abs(mp_obj_t o_in);
-mp_obj_t mp_builtin_all(mp_obj_t o_in);
-mp_obj_t mp_builtin_any(mp_obj_t o_in);
-mp_obj_t mp_builtin_callable(mp_obj_t o_in);
-mp_obj_t mp_builtin_chr(mp_obj_t o_in);
-mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin___repl_print___obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_abs_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_all_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj);
 MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj);
 MP_DECLARE_CONST_FUN_OBJ(mp_builtin_isinstance_obj);
 MP_DECLARE_CONST_FUN_OBJ(mp_builtin_issubclass_obj);
 MP_DECLARE_CONST_FUN_OBJ(mp_builtin_iter_obj);
-mp_obj_t mp_builtin_len(mp_obj_t o_in);
-mp_obj_t mp_builtin_list(int n_args, const mp_obj_t *args);
-mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args);
-mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_len_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_list_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_max_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_min_obj);
 MP_DECLARE_CONST_FUN_OBJ(mp_builtin_next_obj);
-mp_obj_t mp_builtin_ord(mp_obj_t o_in);
-mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args);
-mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args);
-mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args);
-mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_ord_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_range_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 90a0fc3394f37e48c012be7f9f0fe74a6cacd79b..33576e3f0e9febf2b1b6c1c5dff7079bf71ee18d 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -18,7 +18,7 @@
 #include "map.h"
 #include "builtin.h"
 
-mp_obj_t mp_builtin___import__(int n, mp_obj_t *args) {
+mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
     /*
     printf("import:\n");
     for (int i = 0; i < n; i++) {
diff --git a/py/mpconfig.h b/py/mpconfig.h
index ada4aa2ea42f2d784f7002650496b9f8414fdb59..5d8c57692e592dd43359c384eda3b59e919824c7 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -91,7 +91,7 @@ typedef long long mp_longint_impl_t;
 #define BITS_PER_BYTE (8)
 #define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD)
 // machine_int_t value with most significant bit set
-#define WORD_MSBIT_HIGH (1 << (BYTES_PER_WORD * 8 - 1))
+#define WORD_MSBIT_HIGH (((machine_uint_t)1) << (BYTES_PER_WORD * 8 - 1))
 
 // printf format spec to use for machine_int_t and friends
 #ifndef INT_FMT
diff --git a/py/obj.c b/py/obj.c
index 2759437fd7c89b63f1edb2b31c3ca8d7d87704cf..206bf7a24aed921ca5801583129d951f853fda09 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -108,9 +108,15 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
                 return val == 0;
             } else if (o2 == mp_const_true) {
                 return val == 1;
-            } else {
-                return false;
+            } else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
+                // If o2 is long int, dispatch to its virtual methods
+                mp_obj_base_t *o = o2;
+                if (o->type->binary_op != NULL) {
+                    mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1);
+                    return r == mp_const_true ? true : false;
+                }
             }
+            return false;
         }
     } else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
         return false;
diff --git a/py/objfun.c b/py/objfun.c
index afac3889fd3a6dcc308ba223beb6f99243ac0339..ba3ce6279f1022cb6522f016a0fb2219f136de32 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -98,38 +98,13 @@ const mp_obj_type_t fun_native_type = {
     .call_n_kw = fun_native_call_n_kw,
 };
 
-mp_obj_t rt_make_function_0(mp_fun_0_t fun) {
+// fun must have the correct signature for n_args fixed arguments
+mp_obj_t rt_make_function_n(int n_args, void *fun) {
     mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
     o->base.type = &fun_native_type;
-    o->n_args_min = 0;
-    o->n_args_max = 0;
-    o->fun = fun;
-    return o;
-}
-
-mp_obj_t rt_make_function_1(mp_fun_1_t fun) {
-    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
-    o->base.type = &fun_native_type;
-    o->n_args_min = 1;
-    o->n_args_max = 1;
-    o->fun = fun;
-    return o;
-}
-
-mp_obj_t rt_make_function_2(mp_fun_2_t fun) {
-    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
-    o->base.type = &fun_native_type;
-    o->n_args_min = 2;
-    o->n_args_max = 2;
-    o->fun = fun;
-    return o;
-}
-
-mp_obj_t rt_make_function_3(mp_fun_3_t fun) {
-    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
-    o->base.type = &fun_native_type;
-    o->n_args_min = 3;
-    o->n_args_max = 3;
+    o->is_kw = false;
+    o->n_args_min = n_args;
+    o->n_args_max = n_args;
     o->fun = fun;
     return o;
 }
@@ -137,6 +112,7 @@ mp_obj_t rt_make_function_3(mp_fun_3_t fun) {
 mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
     mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
     o->base.type = &fun_native_type;
+    o->is_kw = false;
     o->n_args_min = n_args_min;
     o->n_args_max = ~((machine_uint_t)0);
     o->fun = fun;
@@ -147,6 +123,7 @@ mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
 mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) {
     mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
     o->base.type = &fun_native_type;
+    o->is_kw = false;
     o->n_args_min = n_args_min;
     o->n_args_max = n_args_max;
     o->fun = fun;
diff --git a/py/objint.c b/py/objint.c
index 26d3c0e337185f99e20c2d9a82a388624203336c..c0bf756f1bb81908ce315d70040ffd1aa6a85aa7 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -8,16 +8,7 @@
 #include "mpconfig.h"
 #include "mpqstr.h"
 #include "obj.h"
-
-typedef struct _mp_obj_int_t {
-    mp_obj_base_t base;
-#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
-    mp_longint_impl_t val;
-#endif
-} mp_obj_int_t;
-
-void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
-mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
+#include "objint.h"
 
 // This dispatcher function is expected to be independent of the implementation
 // of long int
@@ -54,11 +45,13 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
 // This is called only for non-SMALL_INT
 mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
     assert(0);
+    return mp_const_none;
 }
 
 // This is called only with strings whose value doesn't fit in SMALL_INT
 mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
     assert(0);
+    return mp_const_none;
 }
 
 mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
@@ -69,6 +62,7 @@ mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
     }
     // TODO: Raise exception
     assert(0);
+    return mp_const_none;
 }
 
 mp_obj_t mp_obj_new_int(machine_int_t value) {
@@ -77,5 +71,6 @@ mp_obj_t mp_obj_new_int(machine_int_t value) {
     }
     // TODO: Raise exception
     assert(0);
+    return mp_const_none;
 }
 #endif
diff --git a/py/objint.h b/py/objint.h
new file mode 100644
index 0000000000000000000000000000000000000000..14cf977be0a663ebdf275b8abfc37837e2b53af5
--- /dev/null
+++ b/py/objint.h
@@ -0,0 +1,9 @@
+typedef struct _mp_obj_int_t {
+    mp_obj_base_t base;
+#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
+    mp_longint_impl_t val;
+#endif
+} mp_obj_int_t;
+
+void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
+mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
diff --git a/py/objstr.c b/py/objstr.c
index be1f00e686b98563884ecc4d62f796a01a3f8711..f48bde600178ead9bfb18e517235b8ec8034f4ff 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -169,8 +169,8 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) {
     const char* haystack = qstr_str(((mp_obj_str_t*)args[0])->qstr);
     const char* needle = qstr_str(((mp_obj_str_t*)args[1])->qstr);
 
-    ssize_t haystack_len = strlen(haystack);
-    ssize_t needle_len = strlen(needle);
+    size_t haystack_len = strlen(haystack);
+    size_t needle_len = strlen(needle);
 
     size_t start = 0;
     size_t end = haystack_len;
@@ -183,14 +183,17 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) {
     }
 
     char *p = strstr(haystack + start, needle);
-    ssize_t pos = -1;
-    if (p) {
-        pos = p - haystack;
+    if (p == NULL) {
+        // not found
+        return MP_OBJ_NEW_SMALL_INT(-1);
+    } else {
+        // found
+        machine_int_t pos = p - haystack;
         if (pos + needle_len > end) {
             pos = -1;
         }
+        return MP_OBJ_NEW_SMALL_INT(pos);
     }
-    return MP_OBJ_NEW_SMALL_INT(pos);
 }
 
 mp_obj_t str_strip(int n_args, const mp_obj_t *args) {
diff --git a/py/runtime.c b/py/runtime.c
index 2af86b6abd641121eb9ce2fdceebc0b3ca946cda..f43e804b404870b287512e610e565cc8c9d09dc2 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -97,7 +97,7 @@ void rt_init(void) {
 
     // built-in core functions
     mp_map_add_qstr(&map_builtins, MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj);
-    mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, rt_make_function_1(mp_builtin___repl_print__));
+    mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj);
 
     // built-in types
     mp_map_add_qstr(&map_builtins, MP_QSTR_bool, (mp_obj_t)&bool_type);
@@ -114,26 +114,26 @@ void rt_init(void) {
     mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type);
     mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type);
 
-    // built-in user functions; TODO covert all to &mp_builtin_xxx's
-    mp_map_add_qstr(&map_builtins, MP_QSTR_abs, rt_make_function_1(mp_builtin_abs));
-    mp_map_add_qstr(&map_builtins, MP_QSTR_all, rt_make_function_1(mp_builtin_all));
-    mp_map_add_qstr(&map_builtins, MP_QSTR_any, rt_make_function_1(mp_builtin_any));
-    mp_map_add_qstr(&map_builtins, MP_QSTR_callable, rt_make_function_1(mp_builtin_callable));
-    mp_map_add_qstr(&map_builtins, MP_QSTR_chr, rt_make_function_1(mp_builtin_chr));
-    mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, rt_make_function_2(mp_builtin_divmod));
+    // built-in user functions
+    mp_map_add_qstr(&map_builtins, MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj);
+    mp_map_add_qstr(&map_builtins, MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj);
+    mp_map_add_qstr(&map_builtins, MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj);
+    mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj);
+    mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj);
+    mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj);
     mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj);
     mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj);
     mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj);
     mp_map_add_qstr(&map_builtins, MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj);
-    mp_map_add_qstr(&map_builtins, MP_QSTR_len, rt_make_function_1(mp_builtin_len));
-    mp_map_add_qstr(&map_builtins, MP_QSTR_max, rt_make_function_var(1, mp_builtin_max));
-    mp_map_add_qstr(&map_builtins, MP_QSTR_min, rt_make_function_var(1, mp_builtin_min));
+    mp_map_add_qstr(&map_builtins, MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj);
+    mp_map_add_qstr(&map_builtins, MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj);
+    mp_map_add_qstr(&map_builtins, MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj);
     mp_map_add_qstr(&map_builtins, MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj);
-    mp_map_add_qstr(&map_builtins, MP_QSTR_ord, rt_make_function_1(mp_builtin_ord));
-    mp_map_add_qstr(&map_builtins, MP_QSTR_pow, rt_make_function_var(2, mp_builtin_pow));
-    mp_map_add_qstr(&map_builtins, MP_QSTR_print, rt_make_function_var(0, mp_builtin_print));
-    mp_map_add_qstr(&map_builtins, MP_QSTR_range, rt_make_function_var(1, mp_builtin_range));
-    mp_map_add_qstr(&map_builtins, MP_QSTR_sum, rt_make_function_var(1, mp_builtin_sum));
+    mp_map_add_qstr(&map_builtins, MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj);
+    mp_map_add_qstr(&map_builtins, MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj);
+    mp_map_add_qstr(&map_builtins, MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj);
+    mp_map_add_qstr(&map_builtins, MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj);
+    mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj);
 
     next_unique_code_id = 1; // 0 indicates "no code"
     unique_codes_alloc = 0;
@@ -587,12 +587,7 @@ mp_obj_t rt_make_function_from_id(int unique_code_id) {
             fun = mp_obj_new_fun_bc(c->n_args, c->n_locals + c->n_stack, c->u_byte.code);
             break;
         case MP_CODE_NATIVE:
-            switch (c->n_args) {
-                case 0: fun = rt_make_function_0(c->u_native.fun); break;
-                case 1: fun = rt_make_function_1((mp_fun_1_t)c->u_native.fun); break;
-                case 2: fun = rt_make_function_2((mp_fun_2_t)c->u_native.fun); break;
-                default: assert(0); fun = mp_const_none;
-            }
+            fun = rt_make_function_n(c->n_args, c->u_native.fun);
             break;
         case MP_CODE_INLINE_ASM:
             fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
diff --git a/py/runtime.h b/py/runtime.h
index ac53e1411038c270b37953f7fc9dadb2974db880..32cb47684f8d77d7962c25fe5933e60bbb091d81 100644
--- a/py/runtime.h
+++ b/py/runtime.h
@@ -12,10 +12,8 @@ void rt_store_global(qstr qstr, mp_obj_t obj);
 mp_obj_t rt_unary_op(int op, mp_obj_t arg);
 mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs);
 mp_obj_t rt_make_function_from_id(int unique_code_id);
-mp_obj_t rt_make_function_0(mp_fun_0_t f);
-mp_obj_t rt_make_function_1(mp_fun_1_t f);
-mp_obj_t rt_make_function_2(mp_fun_2_t f);
-mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t f);
+mp_obj_t rt_make_function_n(int n_args, void *fun); // fun must have the correct signature for n_args fixed arguments
+mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun);
 mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun); // min and max are inclusive
 mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple);
 mp_obj_t rt_call_function_0(mp_obj_t fun);
diff --git a/stm/audio.c b/stm/audio.c
index 34adefbcd6e02717e2b8cff8693f9016bd5f259d..e2aa32b9fceda2b9d47adb81344532e7ae97fd79 100644
--- a/stm/audio.c
+++ b/stm/audio.c
@@ -91,8 +91,8 @@ void audio_init(void) {
 
     // Python interface
     mp_obj_t m = mp_obj_new_module(qstr_from_str_static("audio"));
-    rt_store_attr(m, qstr_from_str_static("dac"), rt_make_function_1(pyb_audio_dac));
-    rt_store_attr(m, qstr_from_str_static("is_full"), rt_make_function_0(pyb_audio_is_full));
-    rt_store_attr(m, qstr_from_str_static("fill"), rt_make_function_1(pyb_audio_fill));
+    rt_store_attr(m, qstr_from_str_static("dac"), rt_make_function_n(1, pyb_audio_dac));
+    rt_store_attr(m, qstr_from_str_static("is_full"), rt_make_function_n(0, pyb_audio_is_full));
+    rt_store_attr(m, qstr_from_str_static("fill"), rt_make_function_n(1, pyb_audio_fill));
     rt_store_name(qstr_from_str_static("audio"), m);
 }
diff --git a/stm/lcd.c b/stm/lcd.c
index 70d1a26423e3acebf3de44559731d9dc460a218b..82e42b779d30bd85b5f69256f75c516b5cf3e1fa 100644
--- a/stm/lcd.c
+++ b/stm/lcd.c
@@ -220,13 +220,13 @@ void lcd_init(void) {
 
     // Python interface
     mp_obj_t m = mp_obj_new_module(qstr_from_str_static("lcd"));
-    rt_store_attr(m, qstr_from_str_static("lcd8"), rt_make_function_2(lcd_draw_pixel_8));
-    rt_store_attr(m, qstr_from_str_static("clear"), rt_make_function_0(lcd_pix_clear));
-    rt_store_attr(m, qstr_from_str_static("get"), rt_make_function_2(lcd_pix_get));
-    rt_store_attr(m, qstr_from_str_static("set"), rt_make_function_2(lcd_pix_set));
-    rt_store_attr(m, qstr_from_str_static("reset"), rt_make_function_2(lcd_pix_reset));
-    rt_store_attr(m, qstr_from_str_static("show"), rt_make_function_0(lcd_pix_show));
-    rt_store_attr(m, qstr_from_str_static("text"), rt_make_function_1(lcd_print));
+    rt_store_attr(m, qstr_from_str_static("lcd8"), rt_make_function_n(2, lcd_draw_pixel_8));
+    rt_store_attr(m, qstr_from_str_static("clear"), rt_make_function_n(0, lcd_pix_clear));
+    rt_store_attr(m, qstr_from_str_static("get"), rt_make_function_n(2, lcd_pix_get));
+    rt_store_attr(m, qstr_from_str_static("set"), rt_make_function_n(2, lcd_pix_set));
+    rt_store_attr(m, qstr_from_str_static("reset"), rt_make_function_n(2, lcd_pix_reset));
+    rt_store_attr(m, qstr_from_str_static("show"), rt_make_function_n(0, lcd_pix_show));
+    rt_store_attr(m, qstr_from_str_static("text"), rt_make_function_n(1, lcd_print));
     rt_store_name(qstr_from_str_static("lcd"), m);
 }
 
diff --git a/stm/main.c b/stm/main.c
index cdc4432c0943081c318ce373344b39d1e68359db..2085182c29fccbb65b6de150de50fab5ed6552b1 100644
--- a/stm/main.c
+++ b/stm/main.c
@@ -812,36 +812,36 @@ soft_reset:
 
     // add some functions to the python namespace
     {
-        rt_store_name(qstr_from_str_static("help"), rt_make_function_0(pyb_help));
+        rt_store_name(qstr_from_str_static("help"), rt_make_function_n(0, pyb_help));
 
         mp_obj_t m = mp_obj_new_module(qstr_from_str_static("pyb"));
-        rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_0(pyb_info));
-        rt_store_attr(m, qstr_from_str_static("sd_test"), rt_make_function_0(pyb_sd_test));
-        rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_0(pyb_stop));
-        rt_store_attr(m, qstr_from_str_static("standby"), rt_make_function_0(pyb_standby));
-        rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_1(pyb_source_dir));
-        rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_1(pyb_main));
-        rt_store_attr(m, qstr_from_str_static("sync"), rt_make_function_0(pyb_sync));
-        rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_0(pyb_gc));
-        rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_1(pyb_delay));
-        rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_1(pyb_led));
+        rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_n(0, pyb_info));
+        rt_store_attr(m, qstr_from_str_static("sd_test"), rt_make_function_n(0, pyb_sd_test));
+        rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_n(0, pyb_stop));
+        rt_store_attr(m, qstr_from_str_static("standby"), rt_make_function_n(0, pyb_standby));
+        rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_n(1, pyb_source_dir));
+        rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_n(1, pyb_main));
+        rt_store_attr(m, qstr_from_str_static("sync"), rt_make_function_n(0, pyb_sync));
+        rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_n(0, pyb_gc));
+        rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_n(1, pyb_delay));
+        rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_n(1, pyb_led));
         rt_store_attr(m, qstr_from_str_static("switch"), (mp_obj_t)&pyb_switch_obj);
-        rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_2(pyb_servo_set));
-        rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_2(pyb_pwm_set));
+        rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_n(2, pyb_servo_set));
+        rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_n(2, pyb_pwm_set));
         rt_store_attr(m, qstr_from_str_static("accel"), (mp_obj_t)&pyb_mma_read_obj);
         rt_store_attr(m, qstr_from_str_static("mma_read"), (mp_obj_t)&pyb_mma_read_all_obj);
         rt_store_attr(m, qstr_from_str_static("mma_mode"), (mp_obj_t)&pyb_mma_write_mode_obj);
-        rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_1(pyb_hid_send_report));
-        rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_0(pyb_rtc_read));
-        rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_0(pyb_rng_get));
-        rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_1(pyb_Led));
-        rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_1(pyb_Servo));
-        rt_store_attr(m, qstr_from_str_static("I2C"), rt_make_function_2(pyb_I2C));
+        rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_n(1, pyb_hid_send_report));
+        rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_n(0, pyb_rtc_read));
+        rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_n(0, pyb_rng_get));
+        rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_n(1, pyb_Led));
+        rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_n(1, pyb_Servo));
+        rt_store_attr(m, qstr_from_str_static("I2C"), rt_make_function_n(2, pyb_I2C));
         rt_store_attr(m, qstr_from_str_static("gpio"), (mp_obj_t)&pyb_gpio_obj);
-        rt_store_attr(m, qstr_from_str_static("Usart"), rt_make_function_2(pyb_Usart));
+        rt_store_attr(m, qstr_from_str_static("Usart"), rt_make_function_n(2, pyb_Usart));
         rt_store_name(qstr_from_str_static("pyb"), m);
 
-        rt_store_name(qstr_from_str_static("open"), rt_make_function_2(pyb_io_open));
+        rt_store_name(qstr_from_str_static("open"), rt_make_function_n(2, pyb_io_open));
     }
 
     // print a message to the LCD
diff --git a/stm/printf.c b/stm/printf.c
index c0fa82e1b0ca9d8c12793a56064ac675c462e124..732e8345268634c831a19d0a3f1674d7bd68cf28 100644
--- a/stm/printf.c
+++ b/stm/printf.c
@@ -213,9 +213,9 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) {
                 // usable. I expect that this will be replaced with something
                 // more appropriate.
                 char dot = '.';
-                double d = va_arg(args, double);
+                mp_float_t d = va_arg(args, double);
                 int left = (int)d;
-                int right = (int)((d - (double)(int)d) * 1000000.0);
+                int right = (int)((d - (mp_float_t)(int)d) * 1000000.0);
                 chrs += pfenv_print_int(pfenv, left, 1, 10, 'a', flags, width);
                 chrs += pfenv_print_strn(pfenv, &dot, 1, flags, width);
                 chrs += pfenv_print_int(pfenv, right, 0, 10, 'a', PF_FLAG_ZERO_PAD, 6);
diff --git a/stm/pybwlan.c b/stm/pybwlan.c
index 09d642d9c81cb66f1b6da034d3552b7e70236ff4..91d71a652bac5a43d2785ee60c4b8d38aac55ea6 100644
--- a/stm/pybwlan.c
+++ b/stm/pybwlan.c
@@ -357,11 +357,11 @@ void pyb_wlan_init(void) {
 
     mp_obj_t m = mp_obj_new_module(qstr_from_str_static("wlan"));
     rt_store_attr(m, qstr_from_str_static("connect"), rt_make_function_var(0, pyb_wlan_connect));
-    rt_store_attr(m, qstr_from_str_static("disconnect"), rt_make_function_0(pyb_wlan_disconnect));
-    rt_store_attr(m, qstr_from_str_static("ip"), rt_make_function_0(pyb_wlan_get_ip));
-    rt_store_attr(m, qstr_from_str_static("get_host"), rt_make_function_1(pyb_wlan_get_host));
-    rt_store_attr(m, qstr_from_str_static("http_get"), rt_make_function_2(pyb_wlan_http_get));
-    rt_store_attr(m, qstr_from_str_static("serve"), rt_make_function_0(pyb_wlan_serve));
+    rt_store_attr(m, qstr_from_str_static("disconnect"), rt_make_function_n(0, pyb_wlan_disconnect));
+    rt_store_attr(m, qstr_from_str_static("ip"), rt_make_function_n(0, pyb_wlan_get_ip));
+    rt_store_attr(m, qstr_from_str_static("get_host"), rt_make_function_n(1, pyb_wlan_get_host));
+    rt_store_attr(m, qstr_from_str_static("http_get"), rt_make_function_n(2, pyb_wlan_http_get));
+    rt_store_attr(m, qstr_from_str_static("serve"), rt_make_function_n(0, pyb_wlan_serve));
     rt_store_name(qstr_from_str_static("wlan"), m);
 }
 
diff --git a/stm/std.h b/stm/std.h
index 587b9f88802b85404fb65ea25bf1257598970a3f..95c606e0584d38fd77a208e87b2f1cc7117ca075 100644
--- a/stm/std.h
+++ b/stm/std.h
@@ -17,6 +17,8 @@ int strncmp(const char *s1, const char *s2, size_t n);
 char *strndup(const char *s, size_t n);
 char *strcpy(char *dest, const char *src);
 char *strcat(char *dest, const char *src);
+char *strchr(const char *s, int c);
+char *strstr(const char *haystack, const char *needle);
 
 int printf(const char *fmt, ...);
 int snprintf(char *str, size_t size, const char *fmt, ...);
diff --git a/stm/string0.c b/stm/string0.c
index 2a5f255971bca2e833cff87251605673c65592d0..d67c5f2b172572acf638e6e788c46a121fe5d2b2 100644
--- a/stm/string0.c
+++ b/stm/string0.c
@@ -108,3 +108,31 @@ char *strcat(char *dest, const char *src) {
     *d = '\0';
     return dest;
 }
+
+// Public Domain implementation of strchr from:
+// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strchr_function
+char *strchr(const char *s, int c)
+{
+    /* Scan s for the character.  When this loop is finished,
+       s will either point to the end of the string or the
+       character we were looking for.  */
+    while (*s != '\0' && *s != (char)c)
+        s++;
+    return ((*s == c) ? (char *) s : 0);
+}
+
+
+// Public Domain implementation of strstr from:
+// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strstr_function
+char *strstr(const char *haystack, const char *needle)
+{
+    size_t needlelen;
+    /* Check for the null needle case.  */
+    if (*needle == '\0')
+        return (char *) haystack;
+    needlelen = strlen(needle);
+    for (; (haystack = strchr(haystack, *needle)) != 0; haystack++)
+        if (strncmp(haystack, needle, needlelen) == 0)
+            return (char *) haystack;
+    return 0;
+}
diff --git a/stm/timer.c b/stm/timer.c
index 2605d4b4bcc9dcd636102dba6508d7eb71a47046..c665a461d07e97e4639c88eb12c873c7415ab459 100644
--- a/stm/timer.c
+++ b/stm/timer.c
@@ -72,10 +72,10 @@ void timer_init(void) {
 
     // Python interface
     mp_obj_t m = mp_obj_new_module(qstr_from_str_static("timer"));
-    rt_store_attr(m, qstr_from_str_static("callback"), rt_make_function_1(timer_py_set_callback));
-    rt_store_attr(m, qstr_from_str_static("period"), rt_make_function_1(timer_py_set_period));
-    rt_store_attr(m, qstr_from_str_static("prescaler"), rt_make_function_1(timer_py_set_prescaler));
-    rt_store_attr(m, qstr_from_str_static("value"), rt_make_function_0(timer_py_get_value));
+    rt_store_attr(m, qstr_from_str_static("callback"), rt_make_function_n(1, timer_py_set_callback));
+    rt_store_attr(m, qstr_from_str_static("period"), rt_make_function_n(1, timer_py_set_period));
+    rt_store_attr(m, qstr_from_str_static("prescaler"), rt_make_function_n(1, timer_py_set_prescaler));
+    rt_store_attr(m, qstr_from_str_static("value"), rt_make_function_n(0, timer_py_get_value));
     rt_store_name(qstr_from_str_static("timer"), m);
 }