Initial (working!) impl of flashing. Various refactorings.