Refactored version with simple debugging possibilites