refactor into separate functions for readability and easier reuse
fill out inversion function and refactor for readability and reuse