Stripped out client-specific content, moved variable definitions inside the functions where they are used