Fixed bug and properly reset flags.