Aome bug fixes and code cleanup