Heavy refactoring and commenting