Minor optimization and bug fixes.