refactoring, minor fixes and changes after testing