inlining/refactoring to clean up and make more readable