refactored and checked for references