Some correction and code cleanup