Refactoring to improve stability, runtime and memory footprint