Finally repaired flash routines.

Some edge cases remain

Signed-off-by: Ian Bell <ian.h.bell@gmail.com>