Continued refactoring. Fixed minor errors