more fixes and optimizations, cleaning up