Refactored test and flash code