move all flash functionality into function