more code refactoring and commenting