Code cleanup and adding factory guard to rule engine