Various updates, code rewrite