Refactor flash code