Added code to apply rules