Rearrange code. Update sample