cleanup.
simplified loops.
optimized array handling, removed double clone().