refactored codes for sonar