Refactored after code analysis