refactoring; added rules engine