some optimizations, still needs work