Update src/functions.c

Several bugs fixed.