Minor bugs fixed and code cleanup