reorganized code to prevent memory delays