Minor speed improvement, loops via stack, refactoring