Bug and code inspection fixes