code cleanup, some optimizations