optimize code for rendercallback - still crashing