fixing update and introducing optimizations
optimizations plus some bugfixes