re 502 code refactoring