complex function refactored codeclimate