Code optimizations, changed code structure