Removed inner functions to separate module for readability