refactor tests and clean flash code