General refactoring to simplify code