Fix setting the flags and remove some unnecessary and bad code